//
//  DoubleExt.swift
//  kano
//
//  Created by Frank on 2024/12/24.
//

import Foundation

extension Double {
    //thousandth separator
    func separatorString() -> String {
        
        let digits = self.truncatingRemainder(dividingBy: 1) == 0 ? 0 : 2
        
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.groupingSeparator = ","
        formatter.minimumFractionDigits = digits
        formatter.maximumFractionDigits = digits
        
        if let formattedNumber = formatter.string(from: self as NSNumber) {
            //print(formattedNumber)
            return formattedNumber
        }
        return ""
        
    }
    
    //字符串string转int
    static func commaStringToInt(_ string: String) -> Int? {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal // 设置为十进制风格
        
        // 将字符串转换为 NSNumber
        if let number = formatter.number(from: string) {
            return number.intValue // 返回 Int 值
        } else {
            return nil // 如果转换失败，返回 nil
        }
    }
}
